/**
 * @author erond
 */

package commonUtility;

import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import java.net.NetworkInterface;

import java.util.Enumeration;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import javax.swing.border.TitledBorder;
import javax.swing.SwingUtilities;


/**
 * This code was edited or generated using CloudGarden's Jigloo
 * SWT/Swing GUI Builder, which is free for non-commercial
 * use. If Jigloo is being used commercially (ie, by a corporation,
 * company or business for any purpose whatever) then you
 * should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details.
 * Use of Jigloo implies acceptance of these licensing terms.
 * A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
 * THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
 * LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
 */


/**
 * Interfaccia grafica per la configurazione iniziale dei server FTP da avviare sulla macchina
 */
public class FtpServerConfig extends javax.swing.JFrame {

	{
		//Set Look & Feel
		try {
			javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

	private static final long serialVersionUID = -4256468524660873110L;
	private JComboBox jComboBox_NetworkInterfaces;
	private JPanel jPanel1;
	private JLabel jLabel3;
	private JLabel jLabel4;
	private JLabel jLabel5;
	private JTextField jTextField_LocalSlavePort;
	private JTextField jTextField_ExternalSlavePort;
	private JTextField jTextField_LocalMasterPort;
	private JTextField jTextField_LocalSlaveIP;
	private JTextField jTextField_ExternalSlaveIP;
	private JTextField jTextField_LocalMasterIP;
	private JLabel jLabel9;
	private JLabel jLabel8;
	private JLabel jLabel7;
	private JLabel jLabel6;
	private JTextField jTextField_ExternalMasterPort;
	private JLabel jLabel12;
	private JLabel jLabel11;
	private JButton jButton_Start;
	private JLabel jLabel10;
	private JTextField jTextField_ExternalMasterIP;
	private JLabel jLabel2;
	private JLabel jLabel1;
	private JCheckBox jCheckBox_EnableLocalSlaveFtpServer;
	private JCheckBox jCheckBox_EnableExternalSlaveFtpServer;
	private JCheckBox jCheckBox_EnableLocalMasterFtpServer;
	private JPanelImage jPanel2;
	private JCheckBox jCheckBox_MasterEsterno;
	private Object semaphore;
	private Vector <String[]> contents;

	/**
	 * Auto-generated main method to display this JFrame
	 */
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				FtpServerConfig inst = new FtpServerConfig();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}

	public FtpServerConfig() {
		super();
		initGUI();
	}

	public FtpServerConfig(Object semaphore) {
		super();
		setSemaphore(semaphore);
		initGUI();
	}

	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			this.setPreferredSize(new java.awt.Dimension(589, 365));
			{
				getContentPane().add(getJPanel1(), BorderLayout.CENTER);
			}
			pack();
			this.setSize(589, 365);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public JComboBox getJComboBox_NetworkInterfaces() {
		if (jComboBox_NetworkInterfaces==null) {
			Vector<String>interf=new Vector<String>();
			NetworkInterfaceIP net = new NetworkInterfaceIP();
			Enumeration<NetworkInterface> interfaces = net.getAvailableNetworkInterfaces();
			while (interfaces.hasMoreElements()) {
				NetworkInterface nif = interfaces.nextElement();
				interf.add(nif.getDisplayName());
			}

			jComboBox_NetworkInterfaces = new JComboBox(interf);
			jComboBox_NetworkInterfaces.setSelectedIndex(0);
			jComboBox_NetworkInterfaces.setPreferredSize(new java.awt.Dimension(90, 28));
		}
		return jComboBox_NetworkInterfaces;
	}

	private JPanel getJPanel1() {
		if(jPanel1 == null) {
			jPanel1 = new JPanel();
			jPanel1.setPreferredSize(new java.awt.Dimension(589, 374));
			jPanel1.add(getJLabel10());
			jPanel1.add(getJComboBox_NetworkInterfaces());
			jPanel1.add(getJLabel12());
			jPanel1.add(getJLabel9());
			jPanel1.add(getJPanel2());
			jPanel1.add(getJLabel11());
			jPanel1.add(getJButton_Start());
		}
		return jPanel1;
	}

	public JCheckBox getJCheckBox_MasterEsterno() {
		if(jCheckBox_MasterEsterno == null) {
			jCheckBox_MasterEsterno = new JCheckBox();
			jCheckBox_MasterEsterno.setText("Enable external IP master FTP server");
			jCheckBox_MasterEsterno.setBounds(20, 39, 252, 33);
			jCheckBox_MasterEsterno.setSelected(true);
		}
		jCheckBox_MasterEsterno.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent evt) {
				jCheckBox_MasterEsternoMouseClicked(evt);
			}
		});
		return jCheckBox_MasterEsterno;
	}

	private JPanelImage getJPanel2() {
		if(jPanel2 == null) {
			jPanel2 = new JPanelImage();
			jPanel2.setLayout(null);
			jPanel2.setAbsolutePathImage("./media/ftp_cable.png");
			jPanel2.setPreferredSize(new java.awt.Dimension(573, 198));
			jPanel2.setName("FTP server selection");
			jPanel2.setBorder(BorderFactory.createTitledBorder(null, "FTP server selection", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION));
			jPanel2.setEnabled(false);
			jPanel2.add(getJLabel5());
			jPanel2.add(getJLabel6());
			jPanel2.add(getJLabel7());
			jPanel2.add(getJLabel8());
			jPanel2.add(getJTextField_ExternalMasterIP());
			jPanel2.add(getJTextField_LocalMasterIP());
			jPanel2.add(getJTextField_ExternalSlaveIP());
			jPanel2.add(getJLabel4());
			jPanel2.add(getJLabel3());
			jPanel2.add(getJLabel2());
			jPanel2.add(getJLabel1());
			jPanel2.add(getJCheckBox_MasterEsterno());
			jPanel2.add(getJCheckBox_EnableLocalMasterFtpServer());
			jPanel2.add(getJCheckBox_EnableExternalSlaveFtpServer());
			jPanel2.add(getJCheckBox_EnableLocalSlaveFtpServer());
			jPanel2.add(getJTextField_ExternalMasterPort());
			jPanel2.add(getJTextField_LocalSlaveIP());
			jPanel2.add(getJTextField_LocalMasterPort());
			jPanel2.add(getJTextField_ExternalSlavePort());
			jPanel2.add(getJTextField_LocalSlavePort());
		}
		return jPanel2;
	}

	public JCheckBox getJCheckBox_EnableLocalMasterFtpServer() {
		if(jCheckBox_EnableLocalMasterFtpServer == null) {
			jCheckBox_EnableLocalMasterFtpServer = new JCheckBox();
			jCheckBox_EnableLocalMasterFtpServer.setText("Enable local IP master FTP server");
			jCheckBox_EnableLocalMasterFtpServer.setBounds(20, 75, 252, 28);
			jCheckBox_EnableLocalMasterFtpServer.setSelected(true);
		}
		jCheckBox_EnableLocalMasterFtpServer.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent evt) {
				jCheckBox_EnableLocalMasterFtpServerMouseClicked(evt);
			}
		});
		return jCheckBox_EnableLocalMasterFtpServer;
	}

	public JCheckBox getJCheckBox_EnableExternalSlaveFtpServer() {
		if(jCheckBox_EnableExternalSlaveFtpServer == null) {
			jCheckBox_EnableExternalSlaveFtpServer = new JCheckBox();
			jCheckBox_EnableExternalSlaveFtpServer.setText("Enable external IP slave FTP server");
			jCheckBox_EnableExternalSlaveFtpServer.setBounds(20, 109, 252, 30);
		}
		jCheckBox_EnableExternalSlaveFtpServer.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent evt) {
				jCheckBox_EnableExternalSlaveFtpServerMouseClicked(evt);
			}
		});
		return jCheckBox_EnableExternalSlaveFtpServer;
	}

	public JCheckBox getJCheckBox_EnableLocalSlaveFtpServer() {
		if(jCheckBox_EnableLocalSlaveFtpServer == null) {
			jCheckBox_EnableLocalSlaveFtpServer = new JCheckBox();
			jCheckBox_EnableLocalSlaveFtpServer.setText("Enable local IP slave FTP server");
			jCheckBox_EnableLocalSlaveFtpServer.setBounds(20, 146, 252, 27);
			jCheckBox_EnableLocalSlaveFtpServer.setSelected(true);
		}
		jCheckBox_EnableLocalSlaveFtpServer.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent evt) {
				jCheckBox_EnableLocalSlaveFtpServerMouseClicked(evt);
			}
		});
		return jCheckBox_EnableLocalSlaveFtpServer;
	}

	private JLabel getJLabel1() {
		if(jLabel1 == null) {
			jLabel1 = new JLabel();
			jLabel1.setText("IP");
			jLabel1.setBounds(307, 43, 17, 21);
		}
		if (getJCheckBox_MasterEsterno().isSelected()) jLabel1.setEnabled(true);
		else jLabel1.setEnabled(false);
		return jLabel1;
	}

	private JLabel getJLabel2() {
		if(jLabel2 == null) {
			jLabel2 = new JLabel();
			jLabel2.setText("IP");
			jLabel2.setBounds(307, 78, 17, 21);
		}
		if (getJCheckBox_EnableLocalMasterFtpServer().isSelected()) jLabel2.setEnabled(true);
		else jLabel2.setEnabled(false);
		return jLabel2;
	}

	private JLabel getJLabel3() {
		if(jLabel3 == null) {
			jLabel3 = new JLabel();
			jLabel3.setText("IP");
			jLabel3.setBounds(307, 114, 17, 21);
		}
		if (getJCheckBox_EnableExternalSlaveFtpServer().isSelected()) jLabel3.setEnabled(true);
		else jLabel3.setEnabled(false);
		return jLabel3;
	}

	private JLabel getJLabel4() {
		if(jLabel4 == null) {
			jLabel4 = new JLabel();
			jLabel4.setText("IP");
			jLabel4.setBounds(307, 149, 17, 21);
		}
		if (getJCheckBox_EnableLocalSlaveFtpServer().isSelected()) jLabel4.setEnabled(true);
		else jLabel4.setEnabled(false);
		return jLabel4;
	}

	public JTextField getJTextField_ExternalMasterIP() {
		if(jTextField_ExternalMasterIP == null) {
			jTextField_ExternalMasterIP = new JTextField();
			jTextField_ExternalMasterIP.setText("192.168.178.54");
			jTextField_ExternalMasterIP.setBounds(336, 40, 115, 29);
		}
		if (getJCheckBox_MasterEsterno().isSelected()) {
			jTextField_ExternalMasterIP.setEnabled(true);
		} else jTextField_ExternalMasterIP.setEnabled(false);
		return jTextField_ExternalMasterIP;
	}

	private JLabel getJLabel5() {
		if(jLabel5 == null) {
			jLabel5 = new JLabel();
			jLabel5.setText("PORT");
			jLabel5.setBounds(469, 45, 34, 16);
		}
		if (getJCheckBox_MasterEsterno().isSelected()) jLabel5.setEnabled(true);
		else jLabel5.setEnabled(false);
		return jLabel5;
	}

	public JTextField getJTextField_ExternalMasterPort() {
		if(jTextField_ExternalMasterPort == null) {
			jTextField_ExternalMasterPort = new JTextField();
			jTextField_ExternalMasterPort.setText("2121");
			jTextField_ExternalMasterPort.setBounds(509, 39, 44, 27);
		}
		if (getJCheckBox_MasterEsterno().isSelected()) {
			jTextField_ExternalMasterPort.setEnabled(true);
		} else jTextField_ExternalMasterPort.setEnabled(false);
		return jTextField_ExternalMasterPort;
	}

	private JLabel getJLabel6() {
		if(jLabel6 == null) {
			jLabel6 = new JLabel();
			jLabel6.setText("PORT");
			jLabel6.setBounds(469, 80, 34, 16);
		}
		if (getJCheckBox_EnableLocalMasterFtpServer().isSelected()) jLabel6.setEnabled(true);
		else jLabel6.setEnabled(false);
		return jLabel6;
	}

	private JLabel getJLabel7() {
		if(jLabel7 == null) {
			jLabel7 = new JLabel();
			jLabel7.setText("PORT");
			jLabel7.setBounds(469, 117, 34, 16);
		}
		if (getJCheckBox_EnableExternalSlaveFtpServer().isSelected()) jLabel7.setEnabled(true);
		else jLabel7.setEnabled(false);
		return jLabel7;
	}

	private JLabel getJLabel8() {
		if(jLabel8 == null) {
			jLabel8 = new JLabel();
			jLabel8.setText("PORT");
			jLabel8.setBounds(469, 151, 34, 16);
		}
		if (getJCheckBox_EnableLocalSlaveFtpServer().isSelected()) jLabel8.setEnabled(true);
		else jLabel8.setEnabled(false);
		return jLabel8;
	}

	private JLabel getJLabel9() {
		if(jLabel9 == null) {
			jLabel9 = new JLabel();
			jLabel9.setPreferredSize(new java.awt.Dimension(263, 80));
			ImageIcon ftp = new ImageIcon("./media/ftp.png");
			jLabel9.setIcon(ftp);
		}
		return jLabel9;
	}

	public JTextField getJTextField_LocalMasterIP() {
		if(jTextField_LocalMasterIP == null) {
			jTextField_LocalMasterIP = new JTextField();
			jTextField_LocalMasterIP.setText("localhost");
			jTextField_LocalMasterIP.setBounds(336, 75, 115, 29);
		}
		if (getJCheckBox_EnableLocalMasterFtpServer().isSelected()) jTextField_LocalMasterIP.setEnabled(true);
		else jTextField_LocalMasterIP.setEnabled(false);
		return jTextField_LocalMasterIP;
	}

	public JTextField getJTextField_ExternalSlaveIP() {
		if(jTextField_ExternalSlaveIP == null) {
			jTextField_ExternalSlaveIP = new JTextField();
			jTextField_ExternalSlaveIP.setText("192.168.178.54");
			jTextField_ExternalSlaveIP.setBounds(336, 110, 115, 30);
		}
		if (getJCheckBox_EnableExternalSlaveFtpServer().isSelected()) jTextField_ExternalSlaveIP.setEnabled(true);
		else jTextField_ExternalSlaveIP.setEnabled(false);
		return jTextField_ExternalSlaveIP;
	}

	public JTextField getJTextField_LocalSlaveIP() {
		if(jTextField_LocalSlaveIP == null) {
			jTextField_LocalSlaveIP = new JTextField();
			jTextField_LocalSlaveIP.setText("localhost");
			jTextField_LocalSlaveIP.setBounds(336, 146, 115, 28);
		}
		if (getJCheckBox_EnableLocalSlaveFtpServer().isSelected()) jTextField_LocalSlaveIP.setEnabled(true);
		else jTextField_LocalSlaveIP.setEnabled(false);
		return jTextField_LocalSlaveIP;
	}

	public JTextField getJTextField_LocalMasterPort() {
		if(jTextField_LocalMasterPort == null) {
			jTextField_LocalMasterPort = new JTextField();
			jTextField_LocalMasterPort.setText("2121");
			jTextField_LocalMasterPort.setBounds(509, 75, 44, 27);
		}
		if (getJCheckBox_EnableLocalMasterFtpServer().isSelected()) jTextField_LocalMasterPort.setEnabled(true);
		else jTextField_LocalMasterPort.setEnabled(false);
		return jTextField_LocalMasterPort;
	}

	public JTextField getJTextField_ExternalSlavePort() {
		if(jTextField_ExternalSlavePort == null) {
			jTextField_ExternalSlavePort = new JTextField();
			jTextField_ExternalSlavePort.setText("2122");
			jTextField_ExternalSlavePort.setBounds(509, 111, 44, 27);
		}
		if (getJCheckBox_EnableExternalSlaveFtpServer().isSelected()) jTextField_ExternalSlavePort.setEnabled(true);
		else jTextField_ExternalSlavePort.setEnabled(false);
		return jTextField_ExternalSlavePort;
	}

	public JTextField getJTextField_LocalSlavePort() {
		if(jTextField_LocalSlavePort == null) {
			jTextField_LocalSlavePort = new JTextField();
			jTextField_LocalSlavePort.setText("2122");
			jTextField_LocalSlavePort.setBounds(509, 146, 44, 26);
		}
		if (getJCheckBox_EnableLocalSlaveFtpServer().isSelected()) jTextField_LocalSlavePort.setEnabled(true);
		else jTextField_LocalSlavePort.setEnabled(false);
		return jTextField_LocalSlavePort;
	}

	private JLabel getJLabel10() {
		if(jLabel10 == null) {
			jLabel10 = new JLabel();
			jLabel10.setText("Network interface");
			jLabel10.setPreferredSize(new java.awt.Dimension(121, 25));
			jLabel10.setFont(new java.awt.Font("AlArabiya",3,12));
		}
		return jLabel10;
	}

	public JButton getJButton_Start() {
		if(jButton_Start == null) {
			jButton_Start = new JButton();
			jButton_Start.setText("..... START .....");
			jButton_Start.setPreferredSize(new java.awt.Dimension(191, 33));
			jButton_Start.setFont(new java.awt.Font("AlArabiya",3,14));
			jButton_Start.addMouseListener(new MouseAdapter() {
				public void mouseClicked(MouseEvent evt) {
					jButton_StartMouseClicked(evt);
				}
			});
		}
		return jButton_Start;
	}

	/**
	 * Metodo scatenato alla pressione del pulsante START che salva i parametri immessi in un vettore
	 * che verrà utilizzato per la configurazione del/dei server FTP da avviare sulla macchina
	 * @param evt
	 */
	private void jButton_StartMouseClicked(MouseEvent evt) {
		
		contents = new Vector<String[]>();
		
		String tmp[] = new String[3];
		
		tmp[0]=getJTextField_ExternalMasterIP().getText();
		tmp[1]=getJTextField_ExternalMasterPort().getText();
		if (getJCheckBox_MasterEsterno().isSelected()) tmp[2]="true";
		else tmp[2]="false";
		contents.add(0, tmp);
		
		tmp = new String[3];
		tmp[0]=getJTextField_LocalMasterIP().getText();
		tmp[1]=getJTextField_LocalMasterPort().getText();
		if (getJCheckBox_EnableLocalMasterFtpServer().isSelected()) tmp[2]="true";
		else tmp[2]="false";
		contents.add(1, tmp);
		
		tmp = new String[3];
		tmp[0]=getJTextField_ExternalSlaveIP().getText();
		tmp[1]=getJTextField_ExternalSlavePort().getText();
		if (getJCheckBox_EnableExternalSlaveFtpServer().isSelected()) tmp[2]="true";
		else tmp[2]="false";
		contents.add(2, tmp);
		
		tmp = new String[3];
		tmp[0]=getJTextField_LocalSlaveIP().getText();
		tmp[1]=getJTextField_LocalSlavePort().getText();
		if (getJCheckBox_EnableLocalSlaveFtpServer().isSelected()) tmp[2]="true";
		else tmp[2]="false";
		contents.add(3, tmp);
		
		synchronized (getSemaphore()) {
			try {
				getSemaphore().notify();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	private JLabel getJLabel11() {
		if(jLabel11 == null) {
			jLabel11 = new JLabel();
			jLabel11.setPreferredSize(new java.awt.Dimension(560, -4));
		}
		return jLabel11;
	}

	private void jCheckBox_MasterEsternoMouseClicked(MouseEvent evt) {
		getJTextField_ExternalMasterIP();
		getJTextField_ExternalMasterPort();
		getJLabel1();
		getJLabel5();
	}

	private void jCheckBox_EnableLocalMasterFtpServerMouseClicked(MouseEvent evt) {
		getJTextField_LocalMasterPort();
		getJTextField_LocalMasterIP();
		getJLabel2();
		getJLabel6();
	}

	private void jCheckBox_EnableExternalSlaveFtpServerMouseClicked(MouseEvent evt) {
		getJTextField_ExternalSlavePort();
		getJLabel7();
		getJTextField_ExternalSlaveIP();
		getJLabel3();
	}

	private void jCheckBox_EnableLocalSlaveFtpServerMouseClicked(MouseEvent evt) {
		getJTextField_LocalSlaveIP();
		getJTextField_LocalSlavePort();
		getJLabel4();
		getJLabel8();
	}

	private JLabel getJLabel12() {
		if(jLabel12 == null) {
			jLabel12 = new JLabel();
			jLabel12.setPreferredSize(new java.awt.Dimension(48, 69));
		}
		return jLabel12;
	}

	/**
	 * @param semaphore the semaphore to set
	 */
	public void setSemaphore(Object semaphore) {
		this.semaphore = semaphore;
	}

	/**
	 * @return the semaphore
	 */
	public Object getSemaphore() {
		return semaphore;
	}

	/**
	 * @param contents the contents to set
	 */
	public void setContents(Vector <String[]> contents) {
		this.contents = contents;
	}

	/**
	 * @return the contents
	 */
	public Vector <String[]> getContents() {
		return contents;
	}

}
